<?php

namespace app\admin\controller;

use app\admin\library\Excel;
use app\common\controller\Backend;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Db;

/**
 * 基本代码
 *
 */
class Codelib extends Backend
{
    /**
     * Codelib模型对象
     * @var \app\admin\model\Codelib
     */
    protected $model = null;
    protected $corplib =null;
    protected $quickSearchField = ['CODETYPE'];
    protected $noNeedLogin      = ['*'];
    protected $defaultSortField = 'CODETYPE,desc';

    protected $preExcludeFields = [''];

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\Codelib;
        $this->corplib =new \app\admin\model\Corplib();
    }
    /**
     * 查看
     */
    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }
        $type=$this->request->get('type');
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where('CODETYPE',$type)
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 添加
     */
    public function add()
    {


        if ($this->request->isPost()) {

            $data = $this->request->post();
            if (!$data) {
                $this->error(__('Parameter %s can not be empty', ['']));
            }

            $data = $this->excludeFields($data);
            if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                $data[$this->dataLimitField] = $this->auth->id;
            }
            $adminInfo          = $this->auth->getInfo();
            $data['OP']=$adminInfo['nickname'];
            $data['OPTIME']=date('Y-m-d H:i:s',time());
            $result = false;
            Db::startTrans();
            try {
                // 模型验证
                if ($this->modelValidate) {
                    $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    if (class_exists($validate)) {
                        $validate = new $validate;
                        if ($this->modelSceneValidate) $validate->scene('add');
                        $validate->check($data);
                    }
                }
                $result = $this->model->save($data);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success(__('Added successfully'));
            } else {
                $this->error(__('No rows were added'));
            }
        }
        $this->error(__('Parameter error'));
    }
    /**
     * 获取危险品等级
     */
    public function dangerlevel(){
        $res=$this->model->where('codetype','dangerlevel')->field('code')->paginate();
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 获取船公司
     */
    public function corp(){
        $res=$this->corplib->field('corp')  ->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 获得国家
     */
    public function widearea(){
        $res = $this->model
            ->field('code')
            ->where('codetype','widearea')
            ->paginate();
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /*
     * 获取公司简称
     */
    public function corps(){
        $res=$this->corplib->field('corp')->where('active',1)->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 获取币种
     */
    public function curr(){
        $res=Db::table('currentrate')->select()->toArray();
        $this->success('', [
            'list'   => $res,
            'total'  => count($res),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 获取信用等级
     */
    public function creditclass(){
        $res=$this->model->field('code')->where('codetype','creditclass')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * 获取付款方式
     */
    public function paymode(){
        $res=$this->model->field('code,codename')->where('codetype','paymode')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * @throws \think\db\exception\DbException
     * 获取公司类型
     */
    public function type(){
        $res=$this->model->field('code,codename')->where('codetype','corptype')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * @throws \think\db\exception\DbException
     * 获取流程类型
     */
    public function approvaltype(){
        $res=$this->model->field('codename')->where('codetype','approvaltype')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * @throws \think\db\exception\DbException
     * 获取項目代碼
     */
    public function complaint(){
        $res=$this->model->field('code,codename')->where('codetype','complaint')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * @throws \think\db\exception\DbException
     * 获取投訴類型
     */
    public function types(){
        $res=$this->model->field('code,codename')->where('codetype','complainttype')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    /**
     * @throws \think\db\exception\DbException
     * 获取可装载类别
     */
    public function load(){
        $res=$this->model->field('code,codename')->where('codetype','dangerlevel')->paginate();;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
    public function now(){
        $wheres=$this->request->get('where');
        $quick=$this->request->get('quick_search');
        $res=$this->model->field('code,codename')->where('CODENAME','like',"%$quick%")->where('codetype',$wheres)->paginate('10');;
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }
//    public function edit()
//    {
//        $get = $this->request->get();
//        $get['id'] = 7;
//        $get['no'] = "2023DZYS0006";
//        $tail = Db::table('DZDGSDETAIL')->where('GSID', $get['id'])->select()->toArray();
//        $field = [];
//        foreach ($tail as $k => $v) {
//            $field[$k] = $v['TABLENAME'] . '.' . $v['COLNAME'];
//        }
//        $fields = implode(',', $field);
//        $data = Db::table('TZ')->field($fields)
//            ->join('BILL', 'TZ.TZID=BILL.TZID')
//            ->join('GOODS', 'TZ.CONTRACTNO=GOODS.GOODSID')
//            ->join('CONLOADPLAN', 'TZ.TZID=CONLOADPLAN.TZID')
//            ->where('BILL.MARKNO', $get['no'])
//            ->select()->toArray();
//        $head = array_column($tail, 'COLLABEL');
//        foreach ($data as &$v) {
//            $v = array_values($v);
//        }
//        // 调用类库，读取excel中的内容
////        Excel::export($head,'',$data,'对账单导出');
////        Excel::exportExcel();
//    }
}